﻿Type=Activity
Version=3.15
@EndOfDesignText@
#Region Module Attributes
	#FullScreen: False
	#IncludeTitle: True
#End Region

'Activity module
Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.

End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.

	Dim HttpClientTool As HttpClient
	Dim SyncDone As Boolean
	
	Dim btnTB As Button
	Dim chkZCXM As CheckBox
	Dim chkZCJL As CheckBox
End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.LoadLayout("FormJZBTSJ")
	Activity.Title = "同步数据"	
	
	HttpClientTool.Initialize("HttpClientTool")
	
	SyncDone = True
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub SyncDataSJ
	Dim sURL,sSQL,sText,sSession,sSyncData,sJsonText As String
	Dim hrRequest As HttpRequest
	Dim DataSetTmp As Cursor
	Dim sbSyncData As StringBuilder
	
	SyncDone = False
	
	If chkZCXM.Checked Then
		sSQL = "SELECT * FROM ZD_ZCXM "
		sSQL = PublicVar.GV_DesTool.Base64encrypt(sSQL)
		sSession = PublicVar.GV_DesTool.Base64encrypt(PublicVar.GV_UserSession)
		
		sText ="Session="& sSession &"&SQL="& sSQL
		
		sURL = PublicVar.GetServerUrl&"/jsondata"
		hrRequest.InitializePost2(sURL,sText.GetBytes("UTF8"))
		hrRequest.Timeout = PublicVar.GV_ConnTimeOut

		If HttpClientTool.Execute(hrRequest, 1) = False Then  Return 
		ProgressDialogShow("正在同步数据--支出项目，请稍候...")	

	End If
	
	If chkZCJL.Checked Then
		Dim inStrem As InputStream
		sbSyncData.Initialize
		
		sSQL = "SELECT * FROM JZ_ZCJL0 WHERE (_FLAG=0 OR _FLAG=2)"
		Log("SQL: " & sSQL)
		DataSetTmp = PublicVar.GV_SQLTool.ExecQuery(sSQL)

		sJsonText = PublicTool.DataSet2JSon(DataSetTmp)

		If sJsonText.Length >0 Then
			sbSyncData.Append("{")
			sbSyncData.Append("""Table"":""JZ_ZCJL0"",")
			sbSyncData.Append("""Data"":[").Append(sJsonText).Append("]")
			sbSyncData.Append("}")
		End If
		
		Log(sbSyncData.ToString)		
		
		sSession = PublicVar.GV_DesTool.Base64encrypt(PublicVar.GV_UserSession)

		sSyncData =PublicVar.GV_DesTool.Base64encrypt(sbSyncData.ToString)
		sText ="Session="& sSession &"&SyncData="& sSyncData
		
		Log(sText)
		inStrem.InitializeFromBytesArray(sText.GetBytes("UTF8"),0,sText.Length)
		
		sURL = PublicVar.GetServerUrl&"/syncdata"
		
		'hrRequest.InitializePost2(sURL,sText.GetBytes("UTF8"))
		hrRequest.InitializePost(sURL,inStrem,sText.Length)
		hrRequest.Timeout = 10000 * 5

		If HttpClientTool.Execute(hrRequest, 2) = False Then  Return 
		ProgressDialogShow("正在同步数据--支出记录，请稍候...")	
	
	End If
	
	
'		{
'		["Table" : "Name1",
'		"Data" : 
'			[
'			{"Col1":"Val11","Col2":"Val12"},
'			{"Col1":"Val21","Col2":"Val22"},
'			]
'		],
'		["Table" : "Name2",
'		"Data" : 
'			[
'			{"Col1":"Val11","Col2":"Val12"},
'			{"Col1":"Val21","Col2":"Val22"},
'			]
'		]		
'		}
	
End Sub

Sub HttpClientTool_ResponseSuccess(Response As HttpResponse, TaskId As Int)    
	Dim sResult,sCode,sData As String
	Dim mapResult As Map
	Dim lsRecords As List
	
	ProgressDialogHide
	sResult = Response.GetString("UTF8").Trim
	
	PublicVar.GV_JSonTool.Initialize(sResult)
	mapResult = PublicVar.GV_JSonTool.NextObject
	
	sCode = mapResult.Get("Code")
	If sCode<>"0" Then
		sResult = mapResult.Get("Message")
		ToastMessageShow("同步数据失败"&CRLF &sResult, True)	
		SyncDone = True
		Return
	End If
	
	sData = mapResult.Get("Data")
	If sData ="" Then
		ToastMessageShow("同步数据失败"&CRLF &"返回的数据为空", True)	
		SyncDone = True
		Return	
	
	End If
	
	sData = PublicVar.GV_DesTool.Base64decrypt(sData)
	PublicVar.GV_JSonTool.Initialize(sData)
	lsRecords = PublicVar.GV_JSonTool.NextArray
	
	If TaskId =1 Then
		SyncZCXM(Response,lsRecords)
		
	Else If TaskId =2 Then
		SyncZCJL(Response,lsRecords)
		
	End If

	
	ToastMessageShow("同步数据成功", True)	
	SyncDone = True
End Sub

Sub HttpClientTool_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)   
	Dim sMsg As String
	ProgressDialogHide
		
  Log(Reason)    
  Log(StatusCode) 
  sMsg = "同步数据失败."    
  If Reason <> Null Then sMsg = sMsg & CRLF & Reason    
  ToastMessageShow (sMsg, True)
	SyncDone = True
End Sub

Sub btnTB_Click
	If PublicVar.GV_UserSession ="" Then
		ToastMessageShow ("离线登陆不能同步数据!", True)
		Return
	End If
	
	If SyncDone = False Then
		ToastMessageShow ("正在同步数据，请稍候!", True)
		Return	
	End If
	
	SyncDataSJ
End Sub

Sub SyncZCXM(Response As HttpResponse,lsRecords As List)
	Dim sSql As String
	Dim sBM,sMC,sDW,sDJ As String
	Dim mapResult As Map
	
		PublicVar.GV_SQLTool.BeginTransaction
		Try
			sSql = "DELETE FROM ZD_ZCXM "
			Log("SQL: " & sSql)
			PublicVar.GV_SQLTool.ExecNonQuery(sSql)
			
			For i = 0 To lsRecords.Size - 1 
				mapResult = lsRecords.Get(i)
				
				sBM = mapResult.Get("BM")
				sMC = mapResult.Get("MC")
				sDW = mapResult.Get("DW")
				sDJ = mapResult.Get("DJ")
				If sDJ ="" Then sDJ = "0"
					
				
				sSql = "INSERT INTO ZD_ZCXM(BM,MC,DW,DJ)VALUES('"&sBM&"','"&sMC&"','"&sDW&"',"&sDJ&") "
				Log("SQL: " & sSql)
				PublicVar.GV_SQLTool.ExecNonQuery(sSql)	
				
			Next
			
			PublicVar.GV_SQLTool.TransactionSuccessful
		
		Catch
			Log(LastException.Message)
			ToastMessageShow (LastException.Message, True)	
		End Try
		PublicVar.GV_SQLTool.EndTransaction

End Sub

Sub SyncZCJL(Response As HttpResponse,lsRecords As List)


End Sub


